﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Common;
using Common.Entities;
using Common.Models;
using MobileSales.Interface;

namespace MobileSales.Presenter
{
    class DailyVisitsPresenter
    {
        private DateTime _visitDate = DateTime.Now;
        private IDailyVisitsView _view = null;
        private VisitRepository _dbVisit = null;

        public DailyVisitsPresenter (IDailyVisitsView view, DateTime dateTime)
        {
            _dbVisit = DataManager.GetInstance().Visists;
            _visitDate = dateTime;
            _view = view;
            _view.DataSource = _dbVisit.GetVisitsByDateWork(_visitDate);

            _view.AddVisit += AddVisitDialog;
            _view.DeleteVisit += DeleteDailyVisit;
            _view.CreateFromShedule += CreateVisitsFromShedule;

        }

        public void CreateVisitsFromShedule(object sender, EventArgs e)
        {
            _dbVisit.LoadFromDate(_visitDate);
            _view.DataSource = _dbVisit.GetVisitsByDateWork(_visitDate);
        }

        public void CancelAddVisitDialog(object sender, EventArgs e)
        {
            _view.CloseAddVisitDialog();
        }

        public void AddVisit(object sender, EventArgs e)
        {
            AddVisitDialog addVisitDialog = (AddVisitDialog)sender;
            Visit visit = new Visit();
            visit.DateVisit = _visitDate.ToString();
            visit.BuyPoint = ((BuyPoint) addVisitDialog.SelectedBuyPoint);
            _dbVisit.CreateVisit(visit);
            _view.DataSource = _dbVisit.GetVisitsByDateWork(_visitDate);
        }

        public void AddVisitDialog(object sender, EventArgs e)
        {
            AddVisitDialog addVisitDialog = new AddVisitDialog();
            addVisitDialog.OK += AddVisit;
            addVisitDialog.Cancel += CancelAddVisitDialog;
            _view.ShowAddVisitDialog(addVisitDialog);
        }

        public void DeleteDailyVisit(object sender, EventArgs e)
        {
            Visit currentVisit = ((List<Visit>) _view.DataSource)[(int)_view.Current];
            _dbVisit.DeleteVisit(currentVisit.IdVisit);
            _view.DataSource = _dbVisit.GetVisitsByDateWork(_visitDate);
        }
    }
}
